package com.ch.base.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

/**
* @author    created by lbq
* @date	     2019年1月3日 上午11:34:53
**/
@RestController
@Slf4j
@RequestMapping("/api/erp/v1/message")
public class SendMessageUtil {

    //编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";
    private static String apikey="d354cd2d037d7c0235265189aa680f36";
    private static String apikey2 ="21d74ad47090e8e65369b109ba1051e4";//驰怀apk
    
    private static String urlll="https://t.cn/EXok5dc";
    private static String texttt="【天猫斑珂旗舰店】尊敬的会员您好，51活动碧然德滤水壶优惠券，戳我领取："+urlll+" 惊喜不断,回T退订.";
    private static String yunying=
    		"【天猫斑珂旗舰店】1年仅1次！博克苏收纳店铺超级品牌日！博克苏专营店全场4-7折，精选收纳用品！全场低至3折起！戳：https://t.cn/EXok5dc  ，回T退订.";
    
    
	
	/**
	 * 发送短信接口
	 * @param mobile
	 * @param text
	 * @return
	 */
    @GetMapping(value="many")
    public static String batchSend(String mobile,String text) {
        Map<String, String> params = new HashMap<String, String>();//请求参数集合
        log.info("ceshi");
        params.put("apikey", apikey);
        params.put("text", text);
        params.put("mobile", mobile); 
        System.out.println("调用手机号为："+mobile);
        return post("https://sms.yunpian.com/v2/sms/batch_send.json",  params);//请自行使用post方式请求,可使用Apache HttpClient
    }
    
    
    
	/**
	 * 运营发送营销短信接口
	 * @param mobile
	 * @param text
	 * @return
	 */
    @GetMapping(value="operation")
    public static String operation(String mobile,String text) {
        Map<String, String> params = new HashMap<String, String>();//请求参数集合
        log.info("ceshi");
        params.put("apikey", apikey2);
        params.put("text", yunying);
        params.put("mobile", mobile); 
        params.put("mobile_stat", "true");
        System.out.println("调用手机号为："+mobile);
        return post("https://sms.yunpian.com/v2/sms/batch_send.json",  params);//请自行使用post方式请求,可使用Apache HttpClient
    }
    
    
    /**
     * 获取短链接统计
     * @return
     */
    @GetMapping("stat")
    public static String stat(){
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey2);
        params.put("sid", "cyOprf");
        params.put("start_time", "2019-04-15 11:30:00");//开始时间
        params.put("start_time", "2019-06-15 11:30:00");//结束时间时间
        return post("https://sms.yunpian.com/v2/short_url/stat.json",  params);//请自行使用post方式请求,可使用Apache HttpClient
    
    
    	
       
    }
    
    
    
    
    
    /**
     * 生成短链接口
     * @return
     * 
     */
    @GetMapping(value="shorten")
    public static String shorten() {
        Map<String, String> params = new HashMap<String, String>();//请求参数集合
        //stat_duration-点击量统计持续时间（天），过期后不再统计，区间[0,30]，默认是30表示统计。
        params.put("apikey", apikey2);
        params.put("long_url", "https://taoquan.taobao.com/coupon/unify_apply.htm?sellerId=1036418115&activityId=15583b1b00ee43ebafc1134d9a684a36");
        params.put("provider", "2");//生成的链接的域名，传入1是yp2.cn，2是t.cn，默认1
        //name -取名方便区分，默认为“02-27 11:11生产的短链接”
        return post("https://sms.yunpian.com/v2/short_url/shorten.json",  params);//请自行使用post方式请求,可使用Apache HttpClient
    }
    
    
    /**
     * 获取点击的手机号
     * 
     */
    @GetMapping("phones")
    public Object phones(){
    	 Map<String, String> params = new HashMap<String, String>();//请求参数集合
    	 params.put("apikey", apikey2);
    	 params.put("sid", "cyOprf");
    	 params.put("start_time", "2019-03-29 11:30:00");
    	 params.put("end_time", "2020-03-29 11:30:00");
    	 return post("https://sms.yunpian.com/v2/short_url/clicked", params);
    }
    
    
    
    
	
	/**
	    * 基于HttpClient 4.3的通用POST方法
	    *
	    * @param url       提交的URL
	    * @param paramsMap 提交<参数，值>Map
	    * @return 提交响应
	    */

	   public static String post(String url, Map < String, String > paramsMap) {
	       CloseableHttpClient client = HttpClients.createDefault();
	       String responseText = "";
	       CloseableHttpResponse response = null;
	       try {
	           HttpPost method = new HttpPost(url);
	           if (paramsMap != null) {
	               List < NameValuePair > paramList = new ArrayList <
	                   NameValuePair > ();
	               for (Map.Entry < String, String > param: paramsMap.entrySet()) {
	                   NameValuePair pair = new BasicNameValuePair(param.getKey(),
	                       param.getValue());
	                   paramList.add(pair);
	               }
	               method.setEntity(new UrlEncodedFormEntity(paramList,ENCODING));
	           }
	           response = client.execute(method);
	           HttpEntity entity = response.getEntity();
	           if (entity != null) {
	               responseText = EntityUtils.toString(entity,ENCODING);
	           }
	       } catch (Exception e) {
	           e.printStackTrace();
	       } finally {
	           try {
	               response.close();
	           } catch (Exception e) {
	               e.printStackTrace();
	           }
	       }
	       return responseText;
	   }
}
